package com.pearl.web.demo.config;

import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.*;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 方式1：直接添加 HttpMessageConverters Bean
     * 优先级最好，比加载默认的都先执行
     */
    @Bean
    public HttpMessageConverters customConverters() {
        return new HttpMessageConverters(new FastJsonHttpMessageConverter());
    }
    /**
     * 配置静态访问资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/mypath/**").addResourceLocations("classpath:/myresources/");
    }


    /**
     * 方式2：实现 configureMessageConverters 方法 ，第二顺序执行
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
        messageConverters.add(0,new FastJsonHttpMessageConverter());
    }


    /**
     * 方式3：实现 extendMessageConverters 方法，第三顺序执行
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(0,new FastJsonHttpMessageConverter());
    }
}
